package cbh.tool.inlet;

import cbh.tool.exception.AnnotationNotFoundException;
import cbh.tool.file.DataAccessObjectBuilder;
import cbh.tool.file.DocumentBuilderInterface;
import cbh.tool.file.MybatisXmlBuilder;
import org.apache.commons.logging.impl.Log4JLogger;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;

/**
 * Created by hx-pc on 16-1-30.
 */
public class ProduceMybatisBaseFileImpl implements ProduceMybatisBaseFile {

    @Override
    public ProduceMybatisBaseFile inlet(String packagePath) throws FileNotFoundException, ClassNotFoundException {
        if(produceMkdir(packagePath)){
        }else {
            throw new FileNotFoundException();
        }
        List<Class> list = scanClass(packagePath);
        if(list != null) {
            produceXmlFile(list);
            produceDaoFile(list);
        }
        return this;
    }

    //初始化的时候，创建文件以及文件目录
    private boolean produceMkdir(String packagePath) {
        String xmlFilePath = (packagePath+".xml").replace(".","/");
        String daoFilePath = (packagePath+".dao").replace(".","/");
        //String repoFilePath = (packagePath+".repo").replace(".","/");
        File xmlMkdir = new File(xmlFilePath);
        File daoMkdir = new File(daoFilePath);
        //File repoMkdir = new File(repoFilePath);
        if(!xmlMkdir.exists()){
            xmlMkdir.mkdirs();
        }
        if(!daoMkdir.exists()){
            daoMkdir.mkdirs();
        }/*
        if(!repoMkdir.exists()){
            repoMkdir.mkdirs();
        }*/else {
            return true;
        }
        return xmlMkdir.list().length == 0 && daoMkdir.list().length == 0 ;
    }

    //扫描类所在的包
    private List<Class> scanClass(String packagePath) throws ClassNotFoundException {
        File file = new File("src/main/java/"+packagePath.replace(".","/"));
        if(!file.exists()){
            file = new File("src/main/"+packagePath.replace(".","/"));
            if(!file.exists()){
                throw new ClassNotFoundException();
            }
        }
        String[] classNames = file.list();
        List<Class> list = new ArrayList<>();
        for (int i = 0 ; i < classNames.length ; i++){
            list.add(Class.forName(packagePath+"."+classNames[i].substring(0,classNames[i].lastIndexOf("."))));
        }
        return list;
    }

    private FutureTask produceXmlFile(List<Class> list) {
        return produceFile(list, new MybatisXmlBuilder());
    }

    private FutureTask produceDaoFile(List<Class> list) {
        return produceFile(list, new DataAccessObjectBuilder());
    }

    private FutureTask produceFile(List<Class> list, DocumentBuilderInterface documentBuilderInterface){
            list.forEach(c -> {
                try {
                    documentBuilderInterface.buildParameter(c).create();
                } catch (AnnotationNotFoundException e) {
                    e.printStackTrace();
                }
            });
        return null;
    }
}
